﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ModelLineUsedRoofSlopeData.h"
#include "DbObjectUtils.h"
#include "GbmpBuiltInParameterDefinitionsNew.h"
#include "ICopyStrategyReporter.h"
#include "IDocument.h"
#include "IElement.h"
#include "IElementParameters.h"
#include "IElementParentReporter.h"
#include "IParameter.h"
#include "IParameterDefinitionLibrary.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

DBOBJECT_DATA_DEFINE(ModelLineUsedRoofSlopeData)
{
    SetInstanceId(ElementId::InvalidID);
    SetIsDefineSlope(false);
    SetSlopeAngle(0.0);
    SetProfileCurveOffset(0.0);
}

gcmp::ModelLineUsedRoofSlopeData::ModelLineUsedRoofSlopeData(IDocument * pDoc, const ElementId & ownerElementId, ElementId id)
    : m_pOwnerElement(nullptr)
{
    SetInstanceId(id);
    SetIsDefineSlope(false);
    SetSlopeAngle(0.0);
    SetProfileCurveOffset(0.0);
    if (pDoc)
    {
        m_pOwnerElement = pDoc->GetElement(ownerElementId);
    }
}

void gcmp::ModelLineUsedRoofSlopeData::UpdateForWeakParentDeletion(const std::set<ElementId>& deletedElementIds)
{
    return;
}

void gcmp::ModelLineUsedRoofSlopeData::ReportParents(IElementParentReporter& reporter) const
{
    if (GetInstanceId().IsValid())
    {
        reporter.ReportStrong(GetInstanceId());
    }
}

void gcmp::ModelLineUsedRoofSlopeData::GetCalculators(ICalculatorCollection * calculators) const
{
}

void gcmp::ModelLineUsedRoofSlopeData::ReportParameterDefinitions(std::vector<int>* pParamDefIds) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pParamDefIds != nullptr, L"pParamDefUids为空",L"GDMPLab",L"2024-03-30");
    pParamDefIds->push_back(PARAMETER_ID(RoofProfileCurveDefineSlopeBuiltInParameter));
    pParamDefIds->push_back(PARAMETER_ID(RoofProfileCurveSlopeAngleValueBuiltInParameter));
    pParamDefIds->push_back(PARAMETER_ID(RoofProfileCurveOffsetBuiltInParameter));
}

OwnerPtr<IParameter> gcmp::ModelLineUsedRoofSlopeData::GetNativeParameter(int paramDefId) const
{
    OwnerPtr<IParameter> param;
    ParameterAttributes pa = {false, false, true};
    if (paramDefId == PARAMETER_ID(RoofProfileCurveDefineSlopeBuiltInParameter))
    {
        //param = NEW_AS_OWNER_PTR(Parameter, GetDocument(), ParameterStorageType::Bool, false, false,
            //true, PARAMETER_UID(RoofProfileCurveDefineSlopeBuiltInParameter),
            //ParameterProcessType::GeneralInput);
        param = IParameter::CreateParameter(GetDocument(), ParameterStorageType::Bool,
            pa, PARAMETER_UID(RoofProfileCurveDefineSlopeBuiltInParameter),
            ParameterProcessType::GeneralInput);
        param->SetValueAsBool(GetIsDefineSlope());
        return TransferOwnership(param);
    }
    else if (paramDefId == PARAMETER_ID(RoofProfileCurveSlopeAngleValueBuiltInParameter))
    {
        IElementParameters* pElementParameters = GetDocument()->GetElement(
            GetOwnerElementId())->GetElementParameters();
        DBG_WARN_AND_RETURN_UNLESS(pElementParameters, nullptr, L"pElementParameters为空",L"GDMPLab",L"2024-03-30");
        param = pElementParameters->GetParameterByUid(
            PARAMETER_UID(RoofProfileCurveDefineSlopeBuiltInParameter));
        DBG_WARN_AND_RETURN_UNLESS(param, nullptr, L"param参数无效",L"GDMPLab",L"2024-03-30");
        // 当“轮廓线定义是否坡度”为真时，坡度角参数才可编辑
        ParameterProcessType processType = param->GetValueAsBool() ? 
            ParameterProcessType::GeneralInput : ParameterProcessType::GeneralOutput;

        //param = NEW_AS_OWNER_PTR(Parameter, GetDocument(), ParameterStorageType::Double, false, false, 
            //true, PARAMETER_UID(RoofProfileCurveSlopeAngleValueBuiltInParameter), processType);
        param = IParameter::CreateParameter(GetDocument(), ParameterStorageType::Double,
            pa, PARAMETER_UID(RoofProfileCurveSlopeAngleValueBuiltInParameter), processType); 
        param->SetValueAsDouble(GetSlopeAngle());
        return TransferOwnership(param);
    }
    else if (paramDefId == PARAMETER_ID(RoofProfileCurveOffsetBuiltInParameter))
    {
        //param = NEW_AS_OWNER_PTR(Parameter, GetDocument(), ParameterStorageType::Double, false, false,
            //true, PARAMETER_UID(RoofProfileCurveOffsetBuiltInParameter), ParameterProcessType::GeneralInput);
        param = IParameter::CreateParameter(GetDocument(), ParameterStorageType::Double,
            pa, PARAMETER_UID(RoofProfileCurveOffsetBuiltInParameter), ParameterProcessType::GeneralInput);
        param->SetValueAsDouble(GetProfileCurveOffset());
        return TransferOwnership(param);
    }

    return nullptr;
}

bool gcmp::ModelLineUsedRoofSlopeData::SetNativeParameter(const IParameter *param, std::wstring* errorMsg)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(param, L"param is null.",L"GDMPLab",L"2024-03-30");

    if (errorMsg)
    {
        errorMsg->clear();
    }

    if (param->GetParameterDefinitionId() == PARAMETER_ID(RoofProfileCurveDefineSlopeBuiltInParameter))
    {
        SetIsDefineSlope(param->GetValueAsBool());
        return true;
    }
    else if (param->GetParameterDefinitionId() == PARAMETER_ID(RoofProfileCurveSlopeAngleValueBuiltInParameter))
    {
        double slopeAngleRad = param->GetValueAsDouble();
        double slopeAngleDeg = MathUtils::RadiansToAngle(slopeAngleRad);
        if (false == Intervald(0, 89.99).Contain(slopeAngleDeg))
        {
            SAFE_INIT_POINTER(errorMsg, GBMP_TR(L"坡度值范围应在[0, 89.99]之间"));
            return false;
        }

        SetSlopeAngle(slopeAngleRad);
        return true;
    }
    else if (param->GetParameterDefinitionId() == PARAMETER_ID(RoofProfileCurveOffsetBuiltInParameter))
    {
        SetProfileCurveOffset(param->GetValueAsDouble());
        return true;
    }

    return false;
}

bool gcmp::ModelLineUsedRoofSlopeData::IsParameterModifiable(int paramDefId) const
{
    return true;
}

void gcmp::ModelLineUsedRoofSlopeData::Report(const gcmp::ICopyContext & copyContext, gcmp::ICopyStrategyReporter& reporter) const
{
    reporter.ReportToCopy(GetOwnerElementId());
    reporter.ReportToKeep(GetInstanceId());
}

IDocument * gcmp::ModelLineUsedRoofSlopeData::GetDocument() const
{
    if (!m_pOwnerElement)
    {
        return nullptr;
    }
    return m_pOwnerElement->GetDocument();
}

ElementId gcmp::ModelLineUsedRoofSlopeData::GetOwnerElementId() const
{
    if (!m_pOwnerElement)
    {
        return ElementId::InvalidID;
    }
    return m_pOwnerElement->GetElementId();
}

gcmp::NdbObject* gcmp::ModelLineUsedRoofSlopeData::GetTopOwnerObject() const
{
    if (!GetDocument())
        return nullptr;
    return quick_cast<NdbObject>(GetDocument()->GetElement(GetOwnerElementId()));
}
